﻿
using Crm.CrmClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Crm.Views
{
    public partial class AddSurvey : ChildWindow
    {
        public int AnketID { get; set; }
        public bool EskiEvrak { get; set; }
        AnketTanimlari Anket;
        CrmClient.CrmServiceClient srv = new CrmClient.CrmServiceClient();

        public AddSurvey()
        {
            InitializeComponent();
            srv.AnketKaydetCompleted += srv_AnketKaydetCompleted;
            srv.AnketBulCompleted += srv_AnketBulCompleted;
            this.Loaded += AddSurvey_Loaded;
        }

        void srv_AnketBulCompleted(object sender, AnketBulCompletedEventArgs e)
        {
            Anket = e.Result as AnketTanimlari;
            txtAnketAdi.Text = Anket.AnketAdi;
            chkGecerli.IsChecked = Anket.Gecerli;
            dataGrid.ItemsSource = Anket.Sorular;
        }

        void AddSurvey_Loaded(object sender, RoutedEventArgs e)
        {
            if (!EskiEvrak)
            {
                Anket = new AnketTanimlari();
                Anket.Sorular = new ObservableCollection<Soru>();
                dataGrid.ItemsSource = Anket.Sorular;
            }
            else
            {
                srv.AnketBulAsync(AnketID);
            }
        }

        void srv_AnketKaydetCompleted(object sender, AnketKaydetCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                this.DialogResult = true;
            }
            else
            {
                MessageBox.Show(e.Result.ToString());
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Anket.AnketAdi = txtAnketAdi.Text;
            Anket.Gecerli = (bool)chkGecerli.IsChecked;
            srv.AnketKaydetAsync(Anket);
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Survey sry = new Survey();
            sry.Show();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Anket.Sorular.Add(new Soru() { Tanim = "" });
        }

        private void btnEkle_Click_1(object sender, RoutedEventArgs e)
        {
            var ctl = e.OriginalSource as Button;
            if (null != ctl)
            {
                var sip = ctl.DataContext as Soru;
                if (null != sip)
                {
                    AddAnswer ans = new AddAnswer(sip);
                    ans.Show();
                }
            }
        }

        private void SoruTemizle(object sender, RoutedEventArgs e)
        {
            var ctl = e.OriginalSource as Button;
            if (null != ctl)
            {
                var sip = ctl.DataContext as Soru;
                if (null != sip)
                {
                    if (sip.ID != 0)
                    {
                        srv.SoruSilAsync(sip.ID);
                    }
                    else
                        Anket.Sorular.Remove(sip);
                }
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (Anket.ID != 0)
            {
                srv.AnketSilAsync(Anket.ID);
                Anket.Sorular = new ObservableCollection<Soru>();
                txtAnketAdi.Text = "";
                chkGecerli.IsChecked = false;
                dataGrid.ItemsSource = Anket.Sorular;
            }
            else
            {
                Anket.Sorular = new ObservableCollection<Soru>();
                txtAnketAdi.Text = "";
                chkGecerli.IsChecked = false;
                dataGrid.ItemsSource = Anket.Sorular;
            }
            MessageBox.Show("Kayıt Silinmiştir..");
        }

    }
}

